package initializ

import (
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"grpc/internal/appconfig"
	"os"
)

type NacosData struct {
	DataNacos appconfig.NacosConfig
}

func InitNacos() {

	os.MkdirAll("./tmp/nacos/log", 0777)
	os.MkdirAll("./tmp/nacos/cache", 0777)

	config := appconfig.Conf.NacosConfig

	//create clientConfig
	clientConfig := constant.ClientConfig{
		NamespaceId:         config.NacosName, //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "tmp/nacos/log",
		CacheDir:            "tmp/nacos/cache",
		LogLevel:            "debug",
	}

	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "console1.nacos.io",
			ContextPath: "/nacos",
			Port:        uint64(config.Port),
			Scheme:      config.Host,
		},
	}

	// Create config client for dynamic configuration
	configClient, _ := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})

	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: config.DataID,
		Group:  config.Group})

	if err != nil {
		panic(err)
		return
	}

	fmt.Println(content)
	//json.Unmarshal([]byte(content), &NacosData{})

}
